package com.example.administrator.tianqi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    private TextView tv_city;
    private TextView tv_weather;
    private TextView tv_day;
    private TextView tv_week;
    private TextView tv_temp;
    private TextView tv_WD;
    private TextView tv_WS;
    private TextView tv_sunrise;
    private TextView tv_sunset;

    //请求厦门天气的API接口
    private String url = "http://api.k780.com:88/?app=weather.today&weaid=xiamen&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        //用Volley解析天气接口
        MyVolley();
    }

    private void initView(){
        tv_city= (TextView) findViewById(R.id.tv_city);
        tv_weather= (TextView) findViewById(R.id.tv_weather);
        tv_day= (TextView) findViewById(R.id.tv_day);
        tv_week= (TextView) findViewById(R.id.tv_week);
        tv_temp= (TextView) findViewById(R.id.tv_temp);
        tv_WD= (TextView) findViewById(R.id.tv_WD);
        tv_WS= (TextView) findViewById(R.id.tv_WS);
        tv_sunrise= (TextView) findViewById(R.id.tv_sunrise);
        tv_sunset= (TextView) findViewById(R.id.tv_sunset);
    }

    private void MyVolley(){
        RequestQueue requestQueue=Volley.newRequestQueue(this);

        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject jsonObject=response.getJSONObject("result");
                    tv_city.setText(jsonObject.getString("citynm"));
                    tv_weather.setText(jsonObject.getString("weather"));
                    tv_day.setText(jsonObject.getString("days"));
                    tv_week.setText(jsonObject.getString("week"));
                    tv_temp.setText(jsonObject.getString("temperature_curr"));
                    tv_WD.setText(jsonObject.getString("wind"));
                    tv_WS.setText(jsonObject.getString("winp"));

                    Log.e("TAG",response.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });

        requestQueue.add(jsonObjectRequest);
        requestQueue.start();
    }

}
